package cn.com.thinvent.dispatch.service.biz.util;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class DataConvertUtils {

    public static<VO, DTO> DTO convertVO2DTO(VO src, DTO target) {
        if (target == null) {
            return null;
        }
        BeanUtils.copyProperties(src, target);
        return target;
    }

    public static<DO, DTO> DO convertDTO2DO(DTO src, DO target) {
        BeanUtils.copyProperties(src, target);
        return target;

    }

    public static <DO, DTO> DTO convertDO2DTO(DO src, Class<DTO> dtoClass) {
        try {
            DTO dto = dtoClass.newInstance();
            BeanUtils.copyProperties(src, dto);
            return dto;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static<DO, DTO> DTO convertDO2DTO(DO src, DTO target) {
        if(src == null) {
			return target;
		}
    		
        if (src.getClass().isArray() && target.getClass().isArray()) {

        }
        BeanUtils.copyProperties(src, target);
        return target;

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void convertDOList2DTOList(List origin, List target, Class targetClazz) {
        if (CollectionUtils.isNotEmpty(origin)) {
            List list = (List) origin;
            for (Object info : list) {
                try {
                    Object dto = targetClazz.newInstance();
                    BeanUtils.copyProperties(info, dto);
                    target.add(dto);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
